home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / StorUtil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  12.4 KB  |  457 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        StorUtil.cpp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Owned by:    Craig Carper
  7.  
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     3/26/96    CC        1333083: RemoveDataInterchangeProperties
  13.                                     removes kODPropOriginalID property.
  14.          <2>     3/15/96    CC        1316917: Changed
  15.                                     RemoveDataInterchangeProperties parameter
  16.                                     name to "keepProxyProperties".
  17.  
  18.     To Do:
  19. */
  20.  
  21. /*
  22.     File:        StorUtil.cpp
  23.  
  24.     Contains:    Utilities for working with ODStorageUnits.
  25.  
  26.     Owned by:    Vincent Lo
  27.  
  28.     Copyright:    © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  29.  
  30.     
  31.     In Progress:
  32.         
  33. */
  34.  
  35. #ifndef _STORUTIL_
  36. #include <StorUtil.h>
  37. #endif
  38.  
  39. #ifndef SOM_ODDraft_xh
  40. #include <Draft.xh>
  41. #endif
  42.  
  43. #ifndef SOM_ODSession_xh
  44. #include <ODSessn.xh>
  45. #endif
  46.  
  47. #ifndef SOM_ODStorageSystem_xh
  48. #include <ODStor.xh>
  49. #endif
  50.  
  51. #ifndef SOM_ODDocument_xh
  52. #include <Document.xh>
  53. #endif
  54.  
  55. #ifndef SOM_ODContainer_xh
  56. #include <ODCtr.xh>
  57. #endif
  58.  
  59. #ifndef SOM_ODStorageUnit_xh
  60. #include <StorageU.xh>
  61. #endif
  62.  
  63. #ifndef SOM_ODStorageUnitView_xh
  64. #include <SUView.xh>
  65. #endif
  66.  
  67. #ifndef SOM_Module_OpenDoc_StdDefs_defined
  68. #include <StdDefs.xh>
  69. #endif
  70.  
  71. #ifndef SOM_Module_OpenDoc_StdProps_defined
  72. #include <StdProps.xh>
  73. #endif
  74.  
  75. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  76. #include "StdTypes.xh"
  77. #endif
  78.  
  79. #ifndef SOM_ODTranslation_xh
  80. #include <Translt.xh>
  81. #endif
  82.  
  83. #ifndef _BARRAY_
  84. #include <BArray.h>
  85. #endif
  86.  
  87. #ifndef _STDTYPIO_
  88. #include <StdTypIO.h>
  89. #endif
  90.  
  91. #ifndef _PLFMFILE_
  92. #include <PlfmFile.h>
  93. #endif
  94.  
  95. #ifndef _EXCEPT_
  96. #include <Except.h>
  97. #endif
  98.  
  99. #ifndef _ODMEMORY_
  100. #include <ODMemory.h>
  101. #endif
  102.  
  103. #ifndef __TEXTEDIT__
  104. #include <TextEdit.h>
  105. #endif
  106.  
  107. #ifndef _TEMPOBJ_
  108. #include <TempObj.h>
  109. #endif
  110.  
  111. //==============================================================================
  112. // ODSUAddPropValue
  113. //==============================================================================
  114.  
  115. void        ODSUAddPropValue(Environment* ev,
  116.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  117. {
  118.     su->AddProperty(ev, prop)->AddValue(ev, val);
  119. }
  120.  
  121. //==============================================================================
  122. // ODSUForceFocus
  123. //==============================================================================
  124.  
  125. void        ODSUForceFocus(Environment* ev, 
  126.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  127. {
  128.     if (prop != kODNULL) {
  129.         if (su->Exists(ev, prop, kODNULL, 0) == kODFalse)
  130.             su->AddProperty(ev, prop);
  131.         else
  132.             su->Focus(ev, prop, kODPosUndefined, kODNULL, 0, kODPosUndefined);
  133.     }
  134.     if (val != kODNULL) { 
  135.         if (su->Exists(ev, prop, val, 0) == kODFalse)
  136.             su->AddValue(ev, val);
  137.         else
  138.             su->Focus(ev, prop, kODPosSame, val, 0, kODPosUndefined);
  139.     }
  140.     // else; // Presumably the caller has already focussed the su somewhere,
  141.              // perhaps midstream. -TC
  142. }
  143.  
  144. //==============================================================================
  145. // ODSUExistsThenFocus
  146. //==============================================================================
  147.  
  148. ODBoolean        ODSUExistsThenFocus(Environment* ev, 
  149.                             ODStorageUnit* su, ODPropertyName prop, ODValueType val)
  150. {
  151.     if (prop == kODNULL && val == kODNULL)
  152.         return kODTrue;
  153.     // else; // Presumably the caller has already focussed the su somewhere,
  154.              // perhaps midstream. Property:kODNULL && ValueType:kODNULL always 'exist'. -TC
  155.              
  156.     if (su->Exists(ev, prop, val, 0))
  157.     {
  158.         if ( val )
  159.             su->Focus(ev,  prop, kODPosSame, val, 0, kODPosSame);
  160.         else
  161.             su->Focus(ev,  prop, kODPosSame, kODNULL, 0, kODPosAll);
  162.         return kODTrue;    
  163.     }
  164.     else
  165.     {
  166.         return kODFalse;
  167.     }
  168. }
  169.  
  170. //==============================================================================
  171. // ODSURemoveProperty
  172. //==============================================================================
  173.  
  174. void ODSURemoveProperty(Environment* ev, ODStorageUnit* su, ODPropertyName prop)
  175. {
  176.     if ( ODSUExistsThenFocus(ev, su, prop, kODNULL) )
  177.         su->Remove(ev);
  178. }
  179.  
  180. //----------------------------------------------------------------------------------------
  181. // GetPlatformFileFromContainer
  182. //----------------------------------------------------------------------------------------
  183. PlatformFile*    GetPlatformFileFromContainer(Environment* ev, ODContainer* container)
  184. {
  185.     ODByteArray    ba = container->GetID(ev);
  186.     PlatformFile*    file = new PlatformFile();
  187.     file->Specify((ODFileSpec*) ba._buffer);
  188.     ODDisposePtr(ba._buffer);
  189.     
  190.     return file;
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // GetODFileSpecFromContainer
  195. //----------------------------------------------------------------------------------------
  196. ODFileSpec GetODFileSpecFromContainer(Environment* ev, ODContainer* container)
  197. {
  198.     ODByteArray    ba = container->GetID(ev);
  199.     ODFileSpec documentSpec = *((ODFileSpec*) ba._buffer);
  200.     ODDisposePtr(ba._buffer); // DMc dispose when done
  201.  
  202.     return documentSpec;
  203. }
  204.  
  205. //----------------------------------------------------------------------------------------
  206. // CreateFileContainer
  207. //----------------------------------------------------------------------------------------
  208. ODContainer*    CreateFileContainer(Environment* ev, ODSession* session, ODFileSpec* fsSpec)
  209. {
  210.     ODByteArray*    ba = CreateByteArray(fsSpec, sizeof(short) + sizeof(long) + fsSpec->name[0] + 1);
  211.     ODContainer*    newContainer = session->GetStorageSystem(ev)->
  212.         CreateContainer(ev,kODDefaultFileContainer /*file->GetContainerType()*/,ba);
  213.     DisposeByteArray(ba);
  214.     
  215.     return newContainer;
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. // GetFileContainer
  220. //----------------------------------------------------------------------------------------
  221. ODContainer*    GetFileContainer(Environment* ev, ODSession* session, ODFileSpec* fsSpec)
  222. {
  223.     ODByteArray*    ba = CreateByteArray(fsSpec, sizeof(short) + sizeof(long) + fsSpec->name[0] + 1);
  224.     ODContainer*    newContainer = session->GetStorageSystem(ev)->
  225.         AcquireContainer(ev,kODDefaultFileContainer /*file->GetContainerType()*/,ba);
  226.     DisposeByteArray(ba);
  227.     
  228.     return newContainer;
  229. }
  230.     
  231. //----------------------------------------------------------------------------------------
  232. // CreateMemoryContainer
  233. //----------------------------------------------------------------------------------------
  234. ODContainer* CreateMemoryContainer(Environment* ev,
  235.                 ODSession* session,
  236.                 ODHandle handle,
  237.                 ODContainerType containerType)
  238. {
  239.     ODByteArray* ba;
  240.     TRY
  241.         ODLockHandle(handle);
  242.         ba = CreateByteArray(&handle, sizeof(ODHandle));
  243.         ODUnlockHandle(handle);
  244.     CATCH_ALL
  245.         ODUnlockHandle(handle);
  246.         RERAISE;
  247.     ENDTRY
  248.  
  249.     ODContainer* newContainer = session->GetStorageSystem(ev)->
  250.         CreateContainer(ev, containerType, ba);
  251.     DisposeByteArray(ba);
  252.     
  253.     return newContainer;
  254. }
  255.  
  256. //----------------------------------------------------------------------------------------
  257. // GetMemoryContainer
  258. //----------------------------------------------------------------------------------------
  259. ODContainer* GetMemoryContainer(Environment* ev, 
  260.                 ODSession* session,
  261.                 ODHandle handle,
  262.                 ODContainerType containerType)
  263. {
  264.     ODByteArray* ba;
  265.     TRY
  266.         ODLockHandle(handle);
  267.         ba = CreateByteArray(&handle, sizeof(ODHandle));
  268.         ODUnlockHandle(handle);
  269.     CATCH_ALL
  270.         ODUnlockHandle(handle);
  271.         RERAISE;
  272.     ENDTRY
  273.  
  274.     ODContainer* newContainer = session->GetStorageSystem(ev)->
  275.         AcquireContainer(ev, containerType, ba);
  276.     DisposeByteArray(ba);
  277.     
  278.     return newContainer;
  279. }
  280.  
  281. //----------------------------------------------------------------------------------------
  282. // StorageUnitGetValue
  283. //----------------------------------------------------------------------------------------
  284. ODULong    StorageUnitGetValue(ODStorageUnit* su,
  285.                             Environment* ev,
  286.                             ODULong    size,
  287.                             ODPtr buffer)
  288. {
  289.     ODByteArray    ba;    
  290.     ODULong bytesRead = su->GetValue(ev, size, &ba);
  291.     ODBlockMove(ba._buffer, buffer, bytesRead);
  292.     ODDisposePtr(ba._buffer);
  293.  
  294.     return bytesRead;
  295. }
  296.  
  297. //----------------------------------------------------------------------------------------
  298. // StorageUnitViewGetValue
  299. //----------------------------------------------------------------------------------------
  300. ODULong    StorageUnitViewGetValue(ODStorageUnitView* suv,
  301.                             Environment* ev,
  302.                             ODULong    size,
  303.                             ODPtr buffer)
  304. {
  305.     ODByteArray    ba;
  306.     
  307.     ODULong bytesRead = suv->GetValue(ev, size, &ba);
  308.     ODBlockMove(ba._buffer, buffer, ba._length);
  309.     ODDisposePtr(ba._buffer);
  310.     
  311.     return bytesRead;
  312. }
  313.  
  314.  
  315. //----------------------------------------------------------------------------------------
  316. // StorageUnitSetValue
  317. //----------------------------------------------------------------------------------------
  318. void    StorageUnitSetValue(ODStorageUnit* su,
  319.                             Environment* ev,
  320.                             ODULong    size,
  321.                             const void *buffer)
  322. {
  323.     ODByteArray ba;
  324.     ba._length = size;
  325.     ba._maximum = size;
  326.     ba._buffer = (octet*) buffer;
  327.     su->SetValue(ev, &ba);
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. // StorageUnitViewSetValue
  332. //----------------------------------------------------------------------------------------
  333. void    StorageUnitViewSetValue(ODStorageUnitView* suv,
  334.                                 Environment* ev,
  335.                                 ODULong    size,
  336.                                 const void *buffer)
  337. {
  338.     ODByteArray ba;
  339.     ba._length = size;
  340.     ba._maximum = size;
  341.     ba._buffer = (octet*) buffer;
  342.     suv->SetValue(ev, &ba);
  343. }
  344.  
  345. //----------------------------------------------------------------------------------------
  346. // StorageUnitSetPromiseValue
  347. //----------------------------------------------------------------------------------------
  348. void    StorageUnitSetPromiseValue(ODStorageUnit* su,
  349.                                     Environment* ev,
  350.                                     ODValueType valueType,
  351.                                     ODULong offset,
  352.                                     ODULong    size,
  353.                                     const void *buffer,
  354.                                     ODPart *sourcePart)
  355. {
  356.     ODByteArray ba;
  357.     ba._length = size;
  358.     ba._maximum = size;
  359.     ba._buffer = (octet*) buffer;
  360.     su->SetPromiseValue(ev, valueType, offset, &ba, sourcePart);
  361. }
  362.  
  363. //----------------------------------------------------------------------------------------
  364. // StorageUnitGetStylFromStyledText
  365. //----------------------------------------------------------------------------------------
  366. ODBoolean StorageUnitGetStylFromStyledText(ODStorageUnit* su,
  367.                             Environment* ev,
  368.                             ODULong* size,
  369.                             ODPtr* styl)
  370. {
  371.     ODBoolean    result = kODFalse;
  372.     ODULong        stxtSize;
  373.     ODULong        stylSize;
  374.     ODUShort    scrpNStyles;
  375.     ODType        applestxt = kODNULL;
  376.  
  377.     const ODPlatformType kODScrapType_stxt = 0x73747874;    // 'stxt'
  378.  
  379.     ODVolatile(applestxt);
  380.     ODVolatile(result);
  381.     
  382.     SOM_TRY
  383.  
  384.         ODTranslation* translation = su->GetSession(ev)->GetTranslation(ev);
  385.         applestxt = translation->GetISOTypeFromPlatformType(ev, kODScrapType_stxt, kODPlatformDataType);
  386.             
  387.         if (ODSUExistsThenFocus(ev, su, kODPropContents, applestxt) )
  388.         {
  389.             stxtSize = su->GetSize(ev);
  390.             if ( stxtSize >= sizeof(ODUShort) )
  391.             {
  392.                 StorageUnitGetValue(su, ev, sizeof(ODUShort), &scrpNStyles);
  393.                 stylSize = (scrpNStyles * sizeof(ScrpSTElement)) + sizeof(ODUShort);
  394.                 if ( stxtSize >= stylSize )
  395.                 {
  396.                     *styl = ODNewPtr(stylSize);
  397.                     *size = stylSize;
  398.                     su->SetOffset(ev, 0);
  399.                     StorageUnitGetValue(su, ev, stylSize, *styl);
  400.                     result = kODTrue;
  401.                 }
  402.             }
  403.         }
  404.         
  405.         delete applestxt;
  406.     
  407.     SOM_CATCH_ALL
  408.             if ( applestxt )
  409.                 delete applestxt;
  410.             result = kODFalse;
  411.     SOM_ENDTRY
  412.  
  413.     return result;
  414. }
  415.  
  416. //------------------------------------------------------------------------------
  417. // GetOriginalCloneKind
  418. //------------------------------------------------------------------------------
  419.  
  420. ODCloneKind GetOriginalCloneKind(Environment* ev, ODDraft* draft)
  421. {
  422.     // If content was put on the clipboard without cloning, there will be no
  423.     // kODPropOriginalCloneKind property in the draft's preferences storage
  424.     // unit.  Assume the original operation was a copy in this case. Since
  425.     // no links cannot be placed on the clipboard directly, there won't be
  426.     // links needing fixup on a paste.
  427.  
  428.     TempODStorageUnit draftProperties = draft->AcquireDraftProperties(ev);
  429.     ODCloneKind cloneKind = ODGetULongProp(ev, draftProperties, kODPropOriginalCloneKind, kODULong);
  430.     
  431.     if ((ODULong)(cloneKind) != 0)
  432.         return cloneKind;
  433.     else
  434.         return kODCloneCopy;
  435. }
  436.  
  437. //------------------------------------------------------------------------------
  438. // RemoveDataInterchangeProperties
  439. //------------------------------------------------------------------------------
  440.  
  441. void RemoveDataInterchangeProperties (Environment* ev,
  442.         ODStorageUnit*    su,
  443.         ODBoolean        keepProxyProperties)
  444. {
  445.     ODSURemoveProperty(ev, su, kODPropLinkSpec);
  446.     ODSURemoveProperty(ev, su, kODPropMouseDownOffset);
  447.     ODSURemoveProperty(ev, su, kODPropCloneKindUsed);
  448.     ODSURemoveProperty(ev, su, kODPropOriginalID);
  449.     if ( !keepProxyProperties )
  450.     {
  451.         ODSURemoveProperty(ev, su, kODPropProxyContents);
  452.         ODSURemoveProperty(ev, su, kODPropContentFrame);
  453.         ODSURemoveProperty(ev, su, kODPropSuggestedFrameShape);
  454.     }
  455. }
  456.  
  457.